package com.hnkywl.sts.dao.organ;


import com.hnkywl.sts.entity.organ.Wls;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午2:50
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class WlsDao extends SimpleHibernateTemplate<Wls,Long> {
    /**
     * 查询物流商列表
     * @param qc       物流商全称
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Wls> findWlsList(String qc,String jc,String lxr,int pageNo, int pageSize){
        String hql = null;
        String lxrHql = "";
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Wls> page = new Page<Wls>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add("%" + qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jc like ? ");
            param.add("%" + jc.trim() + "%");
        }
        if (lxr != null && !lxr.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" lxr like ? ");
            param.add("%" + lxr.trim() + "%");
        }
//        if (lxrId != null) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            lxrHql = ", Lxr lxr ";
//            condition.append(" wls in elements(lxr.qyjgList) and lxr.id = ? ");
//            param.add(lxrId);
//        }
//        if (lxrXm != null&&!lxrXm.isEmpty()) {
//            if (condition.length() != 0)
//                condition.append(" and ");
//            lxrHql = ", Lxr lxr ";
//            condition.append(" wls in elements(lxr.qyjgList) and lxr.xm like ? ");
//            param.add("%" + lxrXm.trim() + "%");
//        }

        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" type = ? ");
        param.add("wls");
        //==============记录数量===============
        hql = "select count(*) from Wls ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Wls ";
        if (condition.length() != 0)
            hql = hql + " where " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    /**
     * 根据属性校验实体
     * @param id
     * @param propertyName
     * @param propertyValue
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Wls where " + propertyName + " = ? and type = 'wls'";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Wls where " + propertyName + " = ? and id <> ? and type = 'wls'";
            return this.existsQuery(hql, propertyValue,id);
        }
    }

    /**
     * 查询所有根节点物流商
     *
     * @return
     */
    public List<Wls> findAllRootWls() {
        return find("from Wls where  type = 'wls' and sjjg = null ");
    }




    /**
     * 查询启用的物流商
     * @return
     */
    public List<Wls> findAllInuse() {
        return this.find("from Wls where type = 'wls' and sfzx = false");
    }

    /**
     * 查询所有的物流商
     * @return
     */
    public List<Wls> findAllWls() {
        return this.find("from Wls where deleted = 0");
    }
}
